package com.tju.resource.oauth.controller;

import com.tju.resource.model.TbCommand;
import com.tju.resource.server.TbCommandService;
import com.tju.resource.tool.result.Result;
import com.tju.resource.tool.tool.ClientParameters;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

/**
 * 指令
 */
@Slf4j
@RestController
public class TbCommandController {
    private static final String prefix = "command";
    private Lock lock = new ReentrantLock(true);
    @Autowired
    private TbCommandService tbCommandService;

    /**
     * 新增指令
     */
    @PostMapping(prefix + "/add")
    @PreAuthorize("hasAuthority('sys:command:add')")
    public Result<Object> add(@RequestBody TbCommand tbCommand) {
        try {
            lock.lock();
            return tbCommandService.insertCommand(tbCommand);
        } catch (Exception e) {
            e.printStackTrace();
            throw e;
        } finally {
            lock.unlock();
        }

    }

    /**
     * 修改指令
     */
    @PostMapping(prefix + "/edit")
    @PreAuthorize("hasAuthority('sys:command:edit')")
    public Result<Object> edit(@RequestBody TbCommand tbCommand) {
        try {
            lock.lock();
            return tbCommandService.updateCommand(tbCommand);
        } catch (Exception e) {
            e.printStackTrace();
            throw e;
        } finally {
            lock.unlock();
        }

    }

    /**
     * 删除
     */
    @PostMapping(prefix + "/delete")
    @PreAuthorize("hasAuthority('sys:command:delete')")
    public Result<Object> delete(@RequestBody TbCommand tbCommand) {
        try {
            lock.lock();
            return tbCommandService.deleteCommand(tbCommand);
        } catch (Exception e) {
            e.printStackTrace();
            throw e;
        } finally {
            lock.unlock();
        }

    }

    /**
     * 查询指令
     */
    @PostMapping(prefix + "/list")
    @PreAuthorize("hasAuthority('sys:command:list')")
    public Result<Object> selectTbOrderList(@RequestBody ClientParameters param) {
        return tbCommandService.selectCommandList(param);
    }
    /**
     * 根据订单号查询生产计划
     */

    @PostMapping(prefix + "/getOrderNumber")
    public Result<Object> getOrderNumber(@RequestBody TbCommand tbCommand) {
        try {
            lock.lock();
            return tbCommandService.getOrderNumber(tbCommand.getOrderNumber());
        } catch (Exception e) {
            e.printStackTrace();
            throw e;
        } finally {
            lock.unlock();
        }

    }

    /**
     * 生产指令号
     */
    @PostMapping(prefix + "/production")
    public Result<Object> productionCmdNumber(@RequestBody ClientParameters param) {
        try {
            lock.lock();
            return tbCommandService.productionCmdNumber(param);
        } catch (Exception e) {
            e.printStackTrace();
            throw e;
        } finally {
            lock.unlock();
        }
    }
}
